//
//  AllLikedUserViewController.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/20.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "AllLikedUserViewController.h"
#import "TAMacro.h"
#import "AFNetworking.h"
#import "LikedUserTableViewCell.h"
#import "AllLikedUserContent.h"
#import "PersonalViewController.h"
@interface AllLikedUserViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITableView *allLikedUserTableView;
@property (nonatomic, strong) NSMutableArray<AllLikedUserContent *> *likedUserArray;

@end

@implementation AllLikedUserViewController

- (void)setID:(NSString *)ID {
    _ID = ID;
    NSLog(@"%@", _ID);
}


- (void)netWork {
    NSString *userid = @"57c01d33a9a36d0621c123f7";
    NSString *text = [NSString stringWithFormat:@"{\"uid\":\"%@\",\"pid\":\"%@\"}", userid, _ID];
    //    NSLog(@"%@", text);
    NSString *utf8Text = [text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
    //    NSLog(@"%@", utf8Text);
    //    NSString *p = [self URLEncodedString:utf8Text];
    //    NSString *parmeters = [NSString stringWithFormat:@"c=Post&m=postDetail&p=%7B%22uid%22%3A%2257c01d33a9a36d0621c123f7%22%2C%22pid%22%3A%225723ebe4a9a36d3340a304b6%22%7D&t=t&v=v105", utf8Text];
    
    NSString *parmeters = [NSString stringWithFormat:@"c=Post&m=getLikeUsers&p=%@&t=t&v=v105", utf8Text];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURLString = @"http://api.zheli.io:9501/";
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    // 设置请求参数的字符串, 自定义
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        
        return parmeters;
    }];
    // 正常post发送
    [manager POST:postURLString parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.likedUserArray = [NSMutableArray array];
        NSArray *array = responseObject[@"content"];
        for (NSDictionary *dic in array) {
            AllLikedUserContent *likedUser = [AllLikedUserContent modelWithDic:dic];
            [_likedUserArray addObject:likedUser];
        }
        [_allLikedUserTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败: %@", error);
    }];
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self netWork];
    [self handleNavigationBar];
    [self createView];
}

- (void)createView {
    
    self.allLikedUserTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _allLikedUserTableView.backgroundColor = [UIColor whiteColor];
    _allLikedUserTableView.delegate = self;
    _allLikedUserTableView.dataSource = self;
    [self.view addSubview:_allLikedUserTableView];
//    [_allLikedUserTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [_allLikedUserTableView registerNib:[UINib nibWithNibName:@"LikedUserTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"LikedUserTableViewCellReuse"];
    _allLikedUserTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _likedUserArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LikedUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LikedUserTableViewCellReuse"];
    cell.likedUser = _likedUserArray[indexPath.row];
    cell.clickFollow = ^() {
        NSLog(@"点击了");
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonalViewController *personalVC = [[PersonalViewController alloc] init];
    [self.navigationController pushViewController:personalVC animated:YES];
    personalVC.postId = _likedUserArray[indexPath.row].ID;
    
}


- (void)handleNavigationBar {
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    _titleLabel.center = CGPointMake(SCREEN_WIDTH / 2, 42);
    _titleLabel.textAlignment = 1;
    _titleLabel.textColor = [UIColor whiteColor];
    [self.backView addSubview:_titleLabel];
    _titleLabel.text = @"赞过的人";
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 27, 30, 30)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.backView addSubview:backButton];
    [backButton addTarget:self action:@selector(popViewControllerAction:) forControlEvents:UIControlEventTouchUpInside];
}

// 返回上一页
- (void)popViewControllerAction: (UIButton *)backButton {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
